package com.abel.bigwater.kmgw

import com.abel.bigwater.data.BcdString
import com.abel.bigwater.dataserver.model.ServiceConfig
import com.abel.bigwater.dataserver.model.qdies.D10Packet
import com.abel.bigwater.dataserver.model.qdies.D10PacketRealTest
import com.abel.bigwater.kmgw.dao.ConfigDao
import com.abel.bigwater.kmgw.dao.FlowDao
import com.abel.bigwater.kmgw.dao.RtuDao
import com.abel.bigwater.mapper.DataMapper
import com.abel.bigwater.mapper.MeterMapper
import com.abel.bigwater.mapper.RtuMapper
import com.alibaba.fastjson.JSON
import io.netty.buffer.Unpooled
import io.netty.channel.embedded.EmbeddedChannel
import org.junit.Test

import org.junit.Assert.*
import org.junit.runner.RunWith
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.context.annotation.ComponentScan
import org.springframework.test.context.junit4.SpringRunner

@SpringBootTest(classes = [QdiesServerHandlerTest::class])
@RunWith(SpringRunner::class)
@ComponentScan(basePackages = ["com.abel.bigwater"])
class QdiesServerHandlerTest {

    @Autowired
    var cfgDao: ConfigDao? = null

    @Autowired
    var flowDao: FlowDao? = null

    @Autowired
    var rtuDao: RtuDao? = null

    @Autowired
    var dataMapper: DataMapper? = null

    @Autowired
    var meterMapper: MeterMapper? = null

    @Autowired
    var rtuMapper: RtuMapper? = null

    var cfg: ServiceConfig? = null

    @Test
    fun channelRead() {
        val buf = Unpooled.buffer().writeBytes(BcdString.toByteArray(HEX_4))

        val handler = QdiesServerHandler(dataMapper, meterMapper, rtuMapper, rtuDao,
                ServiceConfig(cfgId = "qdies", firmId = "41", firmCode = "gx-"),
                flowDao!!, "qides")
        val channel = EmbeddedChannel(QdiesDecoder(), handler)
        channel.writeInbound(buf)

        Thread.sleep(5 * 1000)
    }

    companion object {
        private val lgr = LoggerFactory.getLogger(QdiesServerHandlerTest::class.java)
        const val HEX_1 = """49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 15 20 0b 02 00 00 80
3f ae 16 45 20"""

        const val HEX_2 = """49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 15 25 0b 02 66 66 a2
42 b8 16 46 18"""

        const val HEX_3 = """49 45 53 44 31 30 71 90 39 94 fe fe 68 71 90 39 94 1d 0e 0b 02 aa 19 10 10 23 00 0b 02 33 33 ba
42 1d 16 47 21"""

        const val HEX_4 = """fe fe 68 71 90 39 94 1e 14 00 00 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 18 16 
fe fe 68 71 90 39 94 1e 14 00 00 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 18 16"""
    }
}